package ods_industry_2024.ods_02.extract

import org.apache.hudi.DataSourceWriteOptions.{PARTITIONPATH_FIELD, PRECOMBINE_FIELD, RECORDKEY_FIELD}
import org.apache.hudi.QuickstartUtils.getQuickstartWriteConfigs
import org.apache.spark.sql.SparkSession

import java.util.Properties

object extract02 {
  def main(args: Array[String]): Unit = {
    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("数据抽取第二题")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .enableHiveSupport()
      .getOrCreate()

    val connect=new Properties()
    connect.setProperty("user","root")
    connect.setProperty("password","123456")
    connect.setProperty("driver","com.mysql.jdbc.Driver")

    spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_store?useSSL=false","sku_info",connect)
      .createOrReplaceTempView("data")

    spark.table("ods_ds_hudi_02.sku_info").createOrReplaceTempView("ods")

    val result=spark.sql(
      """
        |select
        |m.*
        |from data as m
        |join ods as o
        |on o.id=m.id
        |where m.create_time > o.create_time
        |or m.id not in (select id from ods)
        |""".stripMargin)

    result.write.mode("append")
      .format("hudi")
      .options(getQuickstartWriteConfigs)
      .option(RECORDKEY_FIELD.key(),"id")
      .option(PRECOMBINE_FIELD.key(),"create_time")
      .option(PARTITIONPATH_FIELD.key(),"etl_date")
      .option("hoodie.table.name","sku_info")
      .save("hdfs://192.168.40.110:9000/user/hive/warehouse/ods_ds_hudi_02.db/sku_info")



    spark.close()
  }

}
